package com.qyb;

/**
 * <strong>实现功能:</strong>
 * <p>
 *     环形队列-数组
 * </p>
 *
 * @author 秦有宝(qinyoubao)
 * @version 1.0.0  2019/11/15 14:17
 */
public class Circle1 {

    public static void main(String[] args) {
        AC ac = new AC(5);

        ac.add("1");
        ac.add("2");
        ac.add("3");
        ac.add("4");
        ac.printS();
        ac.add("5");
        ac.printS();
        System.out.println(ac.take());
        ac.printS();
        ac.add("5");
        ac.add("6");
        ac.add("7");
        ac.printS();
        System.out.println(ac.isEmpty());
        System.out.println(ac.isFull());
        System.out.println(ac.take());
        System.out.println(ac.take());
        System.out.println(ac.take());
        System.out.println(ac.take());
        System.out.println(ac.take());
    }
}


class AC{

    private String[]  ss;
    private int first = 0;
    private int last = 0;
    private int size = 0;
    public AC(int size){
        this.size = size;
        ss = new String[size];

    }

    public boolean isEmpty(){
        if(first == last){
            return true;
        }
        return false;
    }

    public boolean isFull(){
        if((last+1)%size == first){
            return true;
        }
        return false;
    }

    public void add(String s){
        if(isFull()){
           return;
        }
        ss[last] = s;
        last = (last+1)%size;
    }

    public String take(){
        if(isEmpty()){
           throw new RuntimeException("空");
        }
        String s = ss[first];
        first = (first+1)%size;
        return s;
    }

    public void printS(){

        if(isEmpty()){
            return;
        }

        int index = first;
        while (index != last){
            System.out.print(ss[index]);
            index = (index+1)%size;
        }
        System.out.println();
    }


}




